﻿using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Text;
using DirectUI.Mobile.UI.Controls;
using RecorderSample.Controller;
namespace RecorderSample.View
{
  public class AudioItemListPage:Page
    {
      private AudioItemListPageControler _controler;
      private DirectUIPanel _plTitle;
   //   private DirectUIStackScrollPanel _plItems;
      private DirectUIStackPanel _plItems;

      private AudioButtonPanel _btButtonPanel;
      private const int BUTTONPANELHEIGHT = 43;
      private const int TITLEPANELHEIGHT = 35;
      private AudioItem _curentItem;
      public AudioItemListPage():base()
      {
          this.Name = "AudioList";
      }
  
      protected override void InitControl()
      {
          base.InitControl();
          _controler = new AudioItemListPageControler();
          _plTitle = new DirectUIPanel();
          _plTitle.Bounds = new System.Drawing.Rectangle(0, 0, 240, TITLEPANELHEIGHT);
          _plTitle.BackGroundImage = Resource.AudioTop;
    //      _plItems = new DirectUIStackScrollPanel();
          _plItems = new DirectUIStackPanel();

          _plItems.Top = TITLEPANELHEIGHT;
          _plItems.Left = 0;
          _plItems.BackGroundImage = Resource.Audiobg2;
          _plItems.Height = 200;
          _btButtonPanel = new AudioButtonPanel();
          _btButtonPanel.Height = BUTTONPANELHEIGHT;
          _btButtonPanel.Command += new EventHandler(_btButtonPanel_Command);
          this.Controls.Add(_plItems);
          this.Controls.Add(_plTitle);
          this.Controls.Add(_btButtonPanel);

      }

      void _btButtonPanel_Command(object sender, EventArgs e)
      {
          switch (((CommandEventArgs)e).Command)
          {
              case "Record":
                  try
                  {
                      ((AudioRecordPage)((PageManager)this.Container).Pages["Record"]).Start();
                      ((PageManager)this.Container).Switch("Record", Transition.ShiftRight);
                  }
                  catch (Exception ex)
                  {
                      MessageDialog.Show(ex.Message, "", null);
                  }
                  break;
              case"Play":
                  try
                  {
                      if (_curentItem == null)
                      {
                          MessageDialog.Show("请录音", "", null);

                      }
                      if (_curentItem.State == 0)
                      {
                          MessageDialog.Show("上传时不能播放，文件正在使用。", "", null);

                      }
                      _controler.Play(_curentItem.FileName);
                  }
                  catch (Exception ex)
                  {
                      MessageDialog.Show(ex.Message, "", null);
                  }
               //  _curentItem.Play();
                  break;
              case"Stop":
                  try
                  {
                      //_curentItem.Stop();
                   //  _controler.Stop();
                     // _controler.Pause();
                  }
                  catch (Exception ex)
                  {
                      MessageDialog.Show(ex.Message, "", null);
                  }
                  break;
              case"Back":
                  //PageManager.Instance.GoBack();
                  break;
          }
      }
      protected override void OnSizeChanged(Size oldSize, Size newSize)
      {
          base.OnSizeChanged(oldSize, newSize);
          if (_plItems == null)
          {
              return;
          }
          _plItems.Bounds = new Rectangle(0, TITLEPANELHEIGHT, this.Width, this.Height - BUTTONPANELHEIGHT-TITLEPANELHEIGHT+5);
          _btButtonPanel.Bounds = new Rectangle(0, this.Height -BUTTONPANELHEIGHT, this.Width, BUTTONPANELHEIGHT);
         
      }
      public void DeleteItem(AudioItem item)
      {
          _plItems.Controls.Remove(item);
          _plItems.Refresh();
          if (_plItems.Controls.Count != 0)
          {
              ((AudioItem)_plItems.Controls[0]).Expand();
          }
          
      }
      public void AddItem(AudioItem item)
      {
          if (item == null)
          {
              throw new ArgumentException("item can't be null");
          }
          if (_curentItem != null)
          {
              _curentItem.Minimize();
          }
          item.Command += new EventHandler(item_Command);
          _curentItem=item;
          _curentItem.ExpandEvent += new EventHandler(_curentItem_ExpandEvent);
              if (string.IsNullOrEmpty(item.Title))
          {
              item.Title = "Audio File " + _plItems.Controls.Count + 1;
          }
          _plItems.Controls.Add(item);
      
      }
      public DirectUIControlCollection Items
      {
          get
          {
              return _plItems.Controls;
          }
      }
      public override void Dispose()
      {
          base.Dispose();
          try
          {
              String filePath = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase;
              //    filePath = Path.GetDirectoryName(filePath);
              if (Directory.Exists(Path.Combine(filePath, "SoundTemp\\")))
              {
                  Directory.Delete(Path.Combine(filePath, "SoundTemp\\"));
              }
          }
          catch (Exception ex)
          {
          }
      }
      void item_Command(object sender, EventArgs e)
      {
          throw new NotImplementedException();
      }

      void _curentItem_ExpandEvent(object sender, EventArgs e)
      {
          //_curentItem.Minimize();
          if (_curentItem != (AudioItem)sender)
          {
              _curentItem.Minimize();
          }
          _curentItem = (AudioItem)sender;
        
      }
    }
}
